﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.Templates;
using RibbonEditor.AppCode;

namespace RibbonEditor.Templates
{
    public class GroupTemplate : ITemplate, IXmlBuildable, IChildDeletable
    {
        public string ClassName { get; set; }
        public string Id { get; set; }
        public List<Layout> Layouts { get; set; }

        public GroupTemplate()
        {
            this.Layouts = new List<Layout>();
        }

        public GroupTemplate(XmlNode node): this()
        {
            try
            {
                if (XmlHelper.GetAttribute(node, "ClassName").Length > 0)
                {
                    this.ClassName = XmlHelper.GetAttribute(node, "ClassName");
                }

                if (XmlHelper.GetAttribute(node, "Id").Length > 0)
                {
                    this.Id = XmlHelper.GetAttribute(node, "Id");
                }

                foreach (XmlNode layoutNode in node.ChildNodes)
                {
                    if (layoutNode.NodeType == XmlNodeType.Comment)
                        return;

                    Layout layout = new Layout(layoutNode);
                    this.Layouts.Add(layout);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode ribbonTemplates)
        {
            try
            {
                XmlNode groupTemplateNode = XmlHelper.AddNode(ribbonTemplates, "GroupTemplate");
                XmlHelper.AddAttribute(groupTemplateNode, "ClassName", this.ClassName);
                XmlHelper.AddAttribute(groupTemplateNode, "ID", this.Id);

                foreach (Layout layout in this.Layouts)
                {
                    layout.AddNode(groupTemplateNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode groupTemplateNode = new TreeNode("GroupTemplate");
                groupTemplateNode.Tag = this;
                groupTemplateNode.ContextMenuStrip = this.BuildContextMenu(groupTemplateNode);

                foreach (Layout layout in this.Layouts)
                {
                    layout.BuildTreeViewElement(groupTemplateNode);
                }

                parentNode.Nodes.Add(groupTemplateNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildContextMenu(TreeNode relatedNode)
        {
            ToolStripMenuItem tsmi = new ToolStripMenuItem("Add Layout");

            ContextMenuStrip cms = new ContextMenuStrip();
            cms.ItemClicked += new ToolStripItemClickedEventHandler(cms_ItemClicked);
            cms.Tag = relatedNode;
            cms.Items.Add(tsmi);

            return cms;
        }

        void cms_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            try
            {
                TreeNode selectedNode = (TreeNode)e.ClickedItem.GetCurrentParent().Tag;

                Layout layout = new Layout();
                layout.BuildTreeViewElement(selectedNode);
                this.Layouts.Add(layout);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            if (o is Layout)
            {
                this.Layouts.Remove((Layout)o);
            }
        }

        #endregion
    }
}
